<%@page pageEncoding = "utf-8" contentType = "text/html;charset=utf-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<c:set var="tasktablecolumns" value="5" scope="page"/>

<h2><fmt:message key="form.RequestDetails"/></h2>

<div class="messages"><jsp:include page="/messages.jsp"/></div>

<c:if test="${((sessionScope.role == 'leadexecutor' && status == 'request_lead_executor') || sessionScope.role == 'chief')}">
<c:if test = "${requestBean.requestStatus.id != 6}">
<p>
  <a class="control" href="${sessionScope.alias}?_action=requestEditForm&request_id=${requestBean.id}">
    <fmt:message key="Command.edit"/> 
  </a>
</p>    
</c:if>
</c:if>


<table class="form" style="width: 100%" cellpadding="12">
      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label" style="width: 130px;"><fmt:message key="request.id" /></td>
        <td>${requestBean.id}</td>
      </tr>
      
      <tr class="marker">
        <td class="label">Название</td>
        <td> 
          <c:choose>
            <c:when test="${requestBean.name == null}">
              <span style="color: red;">нет данных</span>
            </c:when>
            <c:otherwise>
              <c:out value="${requestBean.name}"/>
            </c:otherwise>
          </c:choose>
        </td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.status" /></td>
        <td><c:out value="${requestBean.requestStatus.status}" /><br>
                
        <c:if test="${((sessionScope.role == 'leadexecutor' && status == 'request_lead_executor') || sessionScope.role == 'chief')}">
        <!-- start status manage block --> 
        <c:if test="${requestBean.requestStatus.id != 6}">
          <c:choose>
            <c:when test="${requestBean.requestStatus.id != 5}">
              <a class="control"
                href="${sessionScope.alias}?_action=setRequestInactive&request_id=${requestBean.id}">
              <fmt:message key="Command.requestInactive" /> </a>
            </c:when>
            <c:otherwise>
              <a class="control"
                href="${sessionScope.alias}?_action=setRequestActive&request_id=${requestBean.id}">
              <fmt:message key="Command.requestActive" /> </a>
            </c:otherwise>
          </c:choose>
          <a class="control"
            href="${sessionScope.alias}?_action=setRequestCompleated&request_id=${requestBean.id}">
          <fmt:message key="Command.requestCompleated" /> </a>
        </c:if>                                     
        <!-- end status manage block -->        
        </c:if>
        <!-- end permission block -->
        
        </td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.priority" /></td>
        <td>
           <c:forEach var = "i" begin = "1" end = "${requestBean.priorityBean.id}">
              <img src = "images/priority_item${i}.gif">
           </c:forEach>
        </td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.regNumber" /></td>
        <td>
          <c:choose>
            <c:when test="${requestBean.regNumber == null}">
              <span style="color: red;">нет данных</span>
            </c:when>
            <c:otherwise>
              <c:out value="${requestBean.regNumber}"/>
            </c:otherwise>
          </c:choose>                    
        </td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.organization" /></td>
        <td><c:out value="${requestBean.organization.fullTitle}" /></td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.department" /></td>
        <td><c:out value="${requestBean.department.name}" /></td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.client" /></td>
        <td><c:out value="${requestBean.client.lastName}" />&nbsp;
        <c:out value="${requestBean.client.firstName}" />&nbsp; <c:out
          value="${requestBean.client.middleName}" />
        (<a href='mailto:<c:out value = "${requestBean.client.email1}"/>'>
           <c:out value="${requestBean.client.email1}" /></a>)
        </td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="client.tel1" /></td>
        <td><c:out value="${requestBean.client.tel1}" default='---' /><br>
        <c:out value="${requestBean.client.tel2}" /><br>
        <c:out value="${requestBean.client.tel3}" /></td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.room" /></td>
        <td><c:out value="${requestBean.room}" /></td>
      </tr>

      <tr style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.text" /></td>
        <td class="textField"><c:out value="${requestBean.clientText}"/></td>
      </tr>

      <tr class="marker">
        <td class="label">
          Текст заявки<br>
          <span class="small">(Заполняется диспетчером)</span>
        </td>
        <td class="textField"><c:out value="${requestBean.dispatcherText}" /></td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.group" /></td>
        <td><c:out value="${requestBean.group.name}" /></td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.requestType" /></td>
        <td><c:out value="${requestBean.requestType.type}" /></td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.regDate" /></td>
        <td><fmt:formatDate value="${requestBean.registrationDate}" pattern="dd.MM.yyyy HH:mm" /></td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.approxEndDate" /></td>
        <td>
          <c:choose>
            <c:when test="${requestBean.approxEndDate == null}">
              <span style="color: red;">нет данных</span>
            </c:when>
            <c:otherwise>
              <fmt:formatDate value="${requestBean.approxEndDate}" pattern="dd.MM.yyyy"/>
            </c:otherwise>
          </c:choose>                    
        </td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.realEndDate" /></td>
        <td>
          <c:choose>
            <c:when test="${requestBean.realEndDate == null}">
              <span style="color: red;">нет данных</span>
            </c:when>
            <c:otherwise>
              <fmt:formatDate value="${requestBean.realEndDate}" pattern="dd.MM.yyyy HH:mm"/>
            </c:otherwise>
          </c:choose>                    
        </td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.leadExecutor" /></td>
        <td>
          <c:choose>
            <c:when test="${requestBean.leadExecutor.id == 0}">
              <span style="color: red;">нет данных</span>
            </c:when>
            <c:otherwise>
              <c:out value="${requestBean.leadExecutor.lastName}"/>&nbsp;
              <c:out value="${requestBean.leadExecutor.firstName}"/>&nbsp; 
              <c:out value="${requestBean.leadExecutor.middleName}"/>
              (<a href="mailto:${requestBean.leadExecutor.email}">
                <c:out value="${requestBean.leadExecutor.email}"/>
              </a>)
            </c:otherwise>
          </c:choose>                    
        </td>
      </tr>


      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="Command.executorsList" /></td>
        <td>
        
        <c:if test="${(sessionScope.role == 'leadexecutor' && status == 'request_lead_executor')}">
          <c:if test = "${requestBean.requestStatus.id != 6}">
          <p>
            <a class = "control" href = "${sessionScope.alias}?_action=addTaskForm&request_id=${requestBean.id}">
              <fmt:message key = "Command.addTask"/>
            </a> 
          </p> 
          </c:if>  
        </c:if>
        
        <!-- executors table -->

        <table width="100%">
          <thead>
            <tr style="border-bottom: 1px #f1f1f1 solid;">
              <th>#</th>
              <th><fmt:message key="task.executor"/></th>
              <th>Название</th>
              <th>Завершить к</th>
              <th><fmt:message key="task.status"/></th>
              
              <c:if test="${(sessionScope.role == 'leadexecutor' && status == 'request_lead_executor')}">
                <c:if test = "${requestBean.requestStatus.id != 6}">
                <th>&nbsp;</th>
                <c:set var="tasktablecolumns" value="6" scope="page"/>
                </c:if>
              </c:if>
              
            </tr>
          </thead>

          <tbody>                          
                <c:forEach var="task" items="${tasksList}">
                  <tr style="border-bottom: 1px #f1f1f1 solid;">
                    <td><c:out value="${task.id}" /></td>
                    <td><c:out value="${task.executor.lastName}"/>&nbsp;<c:out value="${task.executor.firstName}" /></td>
                    <td><c:out value="${task.name}"/></td>
                    <td><fmt:formatDate value="${task.approxEndDate}" pattern="dd.MM.yyyy" /></td>
                    <td><c:out value="${task.status.status}"/></td>
                    
                    <c:if test="${(sessionScope.role == 'leadexecutor' && status == 'request_lead_executor')}">
                    <td>
                      <a href="${sessionScope.alias}?_action=taskDetails&request_id=${requestBean.id}&task_id=${task.id}">
                        <fmt:message key="Command.details" /> 
                      </a>
                    </td>
                    </c:if>
                  </tr>
                </c:forEach>
              
              <c:if test="${fn:length(tasksList) == 0}">
                <tr>
                  <td colspan="${tasktablecolumns}" align="center">
                     Список пуст
                  </td>
                </tr>
              </c:if>
                                       
          </tbody>
        </table>
        
        <!-- end executors table -->
        
        <c:if test="${(sessionScope.role == 'leadexecutor' && status == 'request_lead_executor')}">
          <c:if test = "${requestBean.requestStatus.id != 6}">
            <p>
              <a class = "control" href = "${sessionScope.alias}?_action=addTaskForm&request_id=${requestBean.id}">
                <fmt:message key = "Command.addTask"/>
              </a> 
            </p>  
          </c:if>
        </c:if>        
        </td>
      </tr>

      <tr class="marker">
        <td class="label"><fmt:message key="request.report" /></td>
        <td>
          <c:if test="${(sessionScope.role == 'leadexecutor' && status == 'request_lead_executor') || sessionScope.role == 'chief'}">        
          <c:if test = "${requestBean.requestStatus.id != 6}">
            <a class="control" href="${sessionScope.alias}?_action=requestReportForm&request_id=${requestBean.id}">
              <fmt:message key="Command.writeReport" /> 
            </a>            
          </c:if>  
          </c:if>          
          <p>
            <c:if test="${requestBean.leadExecutorReport == null}">
              <span style="color: red">Отчета нет</span>
            </c:if>
            <c:out value="${requestBean.leadExecutorReport}"/>
          </p>
        </td>
      </tr>

      <tr class="marker" style="background-color: #FAFAFA; border-bottom:1px solid #F1F1F1; border-top:1px solid #F1F1F1;">
        <td class="label"><fmt:message key="request.comment" /></td>
        <td><c:out value="${requestBean.comment}"/></td>
      </tr>      
</table>

<c:if test="${((sessionScope.role == 'leadexecutor' && status == 'request_lead_executor') || sessionScope.role == 'chief')}">
<c:if test = "${requestBean.requestStatus.id != 6}">
<p>
    <a class="control" href="${sessionScope.alias}?_action=requestEditForm&request_id=${requestBean.id}">
      <fmt:message key="Command.edit"/> 
    </a>
</p>    
</c:if>
</c:if>